Skip to content

[clang][deps] Properly capture the global module and '\n' for all module directives #148685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

naveen-seth
Copy link
Contributor

@naveen-seth naveen-seth commented Jul 14, 2025

Previously, the newline after a module directive was not properly captured and printed by clang::printDependencyDirectivesAsSource.

According to P1857R3, each directive must, after skipping horizontal whitespace, appear at the start of a logical line. Because the newline after module directives was missing, this invalidated the following line.
This fixes tests that were previously in violation of P1857R3, including Objective-C directives, which should also comply with P1857R3.

This also ensures that the global module fragment module; is captured by the dependency directives scanner.

With this change, the dependency directive scanner now properly identifies
"module;" as a directive, as per P1857R3.
Previously, the global module fragment was not recognized by the
scanner.
@naveen-seth naveen-seth requested a review from Bigcheese July 14, 2025 17:37
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2025

@llvm/pr-subscribers-clang

Author: Naveen Seth Hanig (naveen-seth)

Changes

With this change, the dependency directive scanner now properly identifies "module;" as a directive, as per P1857R3.
Previously, the global module fragment was not recognized by the scanner.


Full diff: https://github.com/llvm/llvm-project/pull/148685.diff

2 Files Affected:

  • (modified) clang/lib/Lex/DependencyDirectivesScanner.cpp (+7)
  • (modified) clang/unittests/Lex/DependencyDirectivesScannerTest.cpp (+5-4)
diff --git a/clang/lib/Lex/DependencyDirectivesScanner.cpp b/clang/lib/Lex/DependencyDirectivesScanner.cpp
index d894c265a07a2..8822e760274d0 100644
--- a/clang/lib/Lex/DependencyDirectivesScanner.cpp
+++ b/clang/lib/Lex/DependencyDirectivesScanner.cpp
@@ -728,6 +728,13 @@ bool Scanner::lexModule(const char *&First, const char *const End) {
       return false;
     break;
   }
+  case ';': {
+    // Handle the global module fragment `module;`.
+    if (Id == "module" && !Export)
+      break;
+    skipLine(First, End);
+    return false;
+  }
   case '<':
   case '"':
     break;
diff --git a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
index d2ef27155df94..92f6f401ec6b7 100644
--- a/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
+++ b/clang/unittests/Lex/DependencyDirectivesScannerTest.cpp
@@ -1122,16 +1122,17 @@ ort \
     )";
   ASSERT_FALSE(
       minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
-  EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;"
+  EXPECT_STREQ("module;#include \"textual-header.h\"\nexport module m;"
                "exp\\\nort import:l[[rename]];"
                "import<<=3;import a b d e d e f e;"
                "import foo[[no_unique_address]];import foo();"
                "import f(:sefse);import f(->a=3);"
                "<TokBeforeEOF>\n",
                Out.data());
-  ASSERT_EQ(Directives.size(), 11u);
-  EXPECT_EQ(Directives[0].Kind, pp_include);
-  EXPECT_EQ(Directives[1].Kind, cxx_export_module_decl);
+  ASSERT_EQ(Directives.size(), 12u);
+  EXPECT_EQ(Directives[0].Kind, cxx_module_decl);
+  EXPECT_EQ(Directives[1].Kind, pp_include);
+  EXPECT_EQ(Directives[2].Kind, cxx_export_module_decl);
 }
 
 TEST(MinimizeSourceToDependencyDirectivesTest, ObjCMethodArgs) {

@@ -1122,16 +1122,17 @@ ort \
)";
ASSERT_FALSE(
minimizeSourceToDependencyDirectives(Source, Out, Tokens, Directives));
EXPECT_STREQ("#include \"textual-header.h\"\nexport module m;"
EXPECT_STREQ("module;#include \"textual-header.h\"\nexport module m;"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a \n after the module; so that #include starts on a new line.

…ule directives

Previously, the newline after a module directive was not properly
captured (or printed).

According to P1857R3, each directive must, after skipping horizontal
whitespace, be at the start of a logical line.
Because the newline after module directives was missing, this
invalidated the following line.

This also fixes or removes tests that were in violation of P1857R3.
This extends to Objective-C directives, which should also
follow P1857R3.

This also ensures that the global module fragment `module;` is
captured by the dependency directives scanner.
@naveen-seth naveen-seth changed the title [clang][deps] Recognize 'module;' in dependency directive scanner [clang][deps] Properly capture the global module and '\n' for all module directives Jul 16, 2025
@Bigcheese Bigcheese requested a review from akyrtzi July 16, 2025 20:51
Copy link
Contributor

@Bigcheese Bigcheese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I'd also like @akyrtzi to take a look if they have time.

@@ -639,15 +639,12 @@ TEST(MinimizeSourceToDependencyDirectivesTest, AtImport) {
ASSERT_FALSE(minimizeSourceToDependencyDirectives(" @ import A;\n", Out));
EXPECT_STREQ("@import A;\n", Out.data());

ASSERT_FALSE(minimizeSourceToDependencyDirectives("@import A\n;", Out));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check removed?

Copy link
Contributor Author

@naveen-seth naveen-seth Jul 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From P1857R3:

The entire import or module directive (including the closing ;) must be on a single logical line and for module must not come from an #include.

While this paper addresses C++, I've consulted with @Bigcheese on whether Objective-C modules should follow the same rules.
In the same spirit, a line-splice was added to this test:

ASSERT_FALSE(minimizeSourceToDependencyDirectives(
"@import /*x*/ A /*x*/ . /*x*/ B /*x*/ \\n /*x*/ ; /*x*/", Out));
EXPECT_STREQ("@import A.B\\n;\n", Out.data());

This patch currently fails CI due to some clang-scan-deps test failures caused by these changes. I'll fix those issues now. Apologies for missing this earlier.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you keep the check but update what the result of the scanner for this input should be?

Lines following the comment 'Note: Not required to pass CI' can be
removed without the CI failing.
The `TryConsumeToken(tok::eod)` is not required in those places, even
though we know that a tok::eod follows.
This is only for the patch review.
This commit removes the extra `TryConsumeToken(tok::eod)` which were
added in e755d18 and which are not explicitly required for this
fix or to pass the CI.
@naveen-seth naveen-seth requested review from Bigcheese and akyrtzi July 18, 2025 02:17
Comment on lines 2364 to 2365
// Note: Not required to pass CI
TryConsumeToken(tok::eod);
Copy link
Contributor Author

@naveen-seth naveen-seth Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch passes the test-suite without the additional (commented) TryConsumeToken(tok::eod) calls added here, but since this patch adds a tok::eod at the end of every module directive, it also makes sense to consume it.

The next/newest commit (0bc7245) includes only the minimal needed changes and leaves out the unnecessary TryConsumeToken(tok::eod) calls.

I split this out into its own commit so I could get you feedback on this.

@naveen-seth naveen-seth requested a review from yronglin July 18, 2025 18:00
Copy link
Contributor

@Bigcheese Bigcheese left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but I'd also like @yronglin to take a look since it interacts with their patch.

Copy link
Contributor

@yronglin yronglin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you working on this.

@naveen-seth naveen-seth merged commit 6855b9c into llvm:main Jul 19, 2025
9 checks passed
@naveen-seth
Copy link
Contributor Author

Thank you for reviewing!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 19, 2025

LLVM Buildbot has detected a new failure on builder lldb-aarch64-windows running on linaro-armv8-windows-msvc-05 while building clang at step 6 "test".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/10268

Here is the relevant piece of the build log for the reference
Step 6 (test) failure: build (failure)
...
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/2/12 (2271 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/3/12 (2272 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/4/12 (2273 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/5/12 (2274 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/6/12 (2275 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/7/12 (2276 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/8/12 (2277 of 2280)
PASS: lldb-unit :: ValueObject/./LLDBValueObjectTests.exe/9/12 (2278 of 2280)
PASS: lldb-unit :: tools/lldb-server/tests/./LLDBServerTests.exe/0/1 (2279 of 2280)
TIMEOUT: lldb-unit :: Host/./HostTests.exe/6/12 (2280 of 2280)
******************** TEST 'lldb-unit :: Host/./HostTests.exe/6/12' FAILED ********************
Script(shard):
--
GTEST_OUTPUT=json:C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\tools\lldb\unittests\Host\.\HostTests.exe-lldb-unit-8128-6-12.json GTEST_SHUFFLE=0 GTEST_TOTAL_SHARDS=12 GTEST_SHARD_INDEX=6 C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\build\tools\lldb\unittests\Host\.\HostTests.exe
--

Note: This is test shard 7 of 12.

[==========] Running 8 tests from 6 test suites.

[----------] Global test environment set-up.

[----------] 1 test from FileSystemTest

[ RUN      ] FileSystemTest.FileAndDirectoryComponents

[       OK ] FileSystemTest.FileAndDirectoryComponents (0 ms)

[----------] 1 test from FileSystemTest (0 ms total)



[----------] 1 test from HostInfoTest

[ RUN      ] HostInfoTest.GetAugmentedArchSpec

[       OK ] HostInfoTest.GetAugmentedArchSpec (0 ms)

[----------] 1 test from HostInfoTest (1 ms total)



[----------] 2 tests from MainLoopTest

[ RUN      ] MainLoopTest.ReadPipeObject


--
exit: 15

@naveen-seth naveen-seth deleted the dep-directive-scanner-global-module branch July 20, 2025 14:45
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
…dule directives (llvm#148685)

Previously, the newline after a module directive was not properly
captured and printed by `clang::printDependencyDirectivesAsSource`.

According to P1857R3, each directive must, after skipping horizontal
whitespace, appear at the start of a logical line. Because the newline
after module directives was missing, this invalidated the following
line.
This fixes tests that were previously in violation of P1857R3,
including for Objective-C directives, which should also comply with
P1857R3.

This also ensures that the global module fragment `module;` is captured
by the dependency directives scanner.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants